package com.woniu.presell.controller;

import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.woniu.common.util.Result;
import com.woniu.presell.VO.LicenceVO;
import com.woniu.presell.entity.Check;
import com.woniu.presell.entity.Licence;
import com.woniu.presell.entity.Proposer;
import com.woniu.presell.service.LicenceService;
import com.woniu.presell.service.ProposerService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;


/**
 * @program: house-after
 * @description:
 * @author: qqq
 * @create: 2022-12-13 17:26
 **/
@RestController
@RequestMapping("Licences")
public class LicenceController {
    @Autowired
    private LicenceService licenceService;
    @Autowired
    private ProposerService proposerService;
    @PostMapping("/add")
    public Result insertLicence(@RequestBody Map<String,Object> map) throws InvocationTargetException, IllegalAccessException {
        String prostr = JSON.toJSONString(map.get("ProposerForm"));
        Proposer proposer = JSON.parseObject(prostr, Proposer.class);
        String chestr = JSON.toJSONString(map.get("check"));
        Check check = JSON.parseObject(chestr, Check.class);
        int i = licenceService.insertPreAndProAndCheck(new Licence(),proposer,check);
        if(i>0){
            return Result.ok("登记成功");
        }else{
            return Result.error("登记失败,请重试");
        }
    }
    @GetMapping("/queryLid/{id}")
    public Result queryLid(@PathVariable Long id){
        Licence licence = licenceService.selectLid(id);
        if(licence!=null){
            return Result.ok().data("licence",licence);
        }else{
            return Result.error("查找失败请重试");
        }

    }
    @GetMapping("/queryPid/{id}")
    public Result queryPid(@PathVariable Long id){
        LicenceVO licenceVO = proposerService.selectById(id);
        if(licenceVO!=null){
            return Result.ok().data("licenceVO",licenceVO);
        }else{
            return Result.error("载入失败请重试");
        }

    }
    @GetMapping("/update/{id}")
    public Result updateid(@PathVariable Integer id){
        licenceService.updateByHigher(id);
        int i = licenceService.updateByid(id);
        if(i>0){
            return Result.ok("归档成功");
        }else{
            return Result.error("载入失败请重试");
        }

    }
}
